subroutine ReadRestart()
use Data_cfoam, only: processor,ie, rank,nump,ierr,outRestarts, nResOut, irestart,String7Number
implicit none
    include 'mpif.h'

    integer :: i
    integer :: ierr1,ierr2
    logical :: ResCellsExists, ResFacesExists
    

    nResOut = irestart

    do
        call Number2String(String7Number,nResOut)
        inquire(FILE=processor(1:ie)//'ResCells'//String7Number(1:7),exist=ResCellsExists)
        inquire(FILE=processor(1:ie)//'ResFaces'//String7Number(1:7),exist=ResFacesExists)
        
        if(ResCellsExists.and.ResFacesExists) then

        else
            if(nResOut.eq.irestart)then
                write(*,*) '@@FATAL ERROR: there is no restart with number', nResOut, ' rank=',rank
                STOP '@@FATAL ERROR: there are no restart files'
            end if
            nResOut = nResOut - 2
            exit
        end if
    end do

    do
        call ReadRestartFile(ierr)

        ierr1 = abs(ierr)

        if (nump.gt.1) then
            call MPI_ALLREDUCE(ierr1,ierr2,1,MPI_DOUBLE_PRECISION,MPI_MAX,MPI_COMM_WORLD,ierr)
!            call MPI_Barrier(MPI_COMM_WORLD,ierr)
            ierr = ierr2
        end if

        if(ierr.eq.0)then
            if(rank.eq.0)then
                write(*,*) 'Restarts (ResCells'//String7Number(1:7),', ', &
                           'ResFaces'//String7Number(1:7), ') have been read successfully!'
            end if

            return
        end if

        nResOut = nResOut - 2
        if(nResOut.le.0)then
            write(*,*) '@@FATAL ERROR: errors in restart files', nResOut, ' rank=',rank
            STOP '@@FATAL ERROR: errors in restart files'
        end if
    end do

    close(32)
    close(outRestarts)

end subroutine ReadRestart

subroutine ReadRestartFile(ierr)
use Data_cfoam, only: String7Number, processor,ie, outRestarts, nResOut, &
                      U,p,T, Usnew,psnew,Rhosnew, utmp
use DataTime, only: ttime,dt,nStepInitial
use Data_Mesh, only: nCells,nFaces
implicit none
    integer,intent(out) :: ierr
    
    integer :: inputRestart
    integer :: i,j
    integer :: nBeginCell,nCellsRead, nBeginFace,nFacesRead
    integer :: OpenFile

    call Number2String(String7Number,nResOut)

    open(32,file=processor(1:ie)//'ResCells'//String7Number(1:7),form='unformatted',IOSTAT=OpenFile)
    if (OpenFile.ne.0) then
	    write(*,*) 'Fatal Error: File open error '//processor(1:ie)//'ResCells'//String7Number(1:7)
	    return
    end if

    open(outRestarts,file=processor(1:ie)//'ResFaces'//String7Number(1:7),form='unformatted',IOSTAT=OpenFile)
    if (OpenFile.ne.0) then
	    write(*,*) 'Fatal Error: File open error '//processor(1:ie)//'ResFaces'//String7Number(1:7)
	    return
    end if


    read(32,iostat=ierr) ttime,i,nCells
    if(ierr.ne.0)then
        close(32)
        close(outRestarts)
        return
    end if
        
    read(32,iostat=ierr) (U(1:3,i),p(i),T(i),i=1,nCells)
    if(ierr.ne.0)then
        close(32)
        close(outRestarts)
        return
    end if

    read(outRestarts,iostat=ierr) ttime,nStepInitial,dt,i,nFaces
    if(ierr.ne.0)then
        close(32)
        close(outRestarts)
        return
    end if

    read(outRestarts,iostat=ierr) (Usnew(1:3,i),psnew(i),Rhosnew(i),i=1,nFaces)
    if(ierr.ne.0)then
        close(32)
        close(outRestarts)
        return
    end if

    read(outRestarts,iostat=ierr) i,j
    if(ierr.ne.0)then
        close(32)
        close(outRestarts)
        return
    end if

    read(outRestarts,iostat=ierr) (utmp(1:8,i),i=1,100)
    if(ierr.ne.0)then
        close(32)
        close(outRestarts)
        return
    end if

    close(32)
    close(outRestarts)

    return
end subroutine ReadRestartFile